/**
 * 用户业务中间件
 * @type {User}
 */
const User = require('../model/user');

const fs = require('fs');

module.exports = {

  /**
   * 用户登录中间件
   * @param request
   * @param response
   * @param next
   */
  gUserByUnameAndPwd: (request,response,next) => {
    console.log('userMid', request.body);
    console.log(request.body);
    let {username , password} = request.body //表单post请求的参数获取方式
    console.log('end='+username);
    console.log('end='+password);
    User.getUserByUnameAndPwd(username , password)
      .then(results => {   //登录成功
        request.user = results; //把查询的结果放到request中并命名为user
        next();
      })
      .catch(error => {
        next(error);
      })
  },

  /**
   * 通过用户名查询用户中间件
   * @param request
   * @param response
   * @param next
   */
  gUserByUsername: (request, response, next) => {
    let {username} = request.query;
    User.getUserByUsername(username)
      .then(result => {
        request.user = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定用户的记录条数中间件
   * @param request
   * @param response
   * @param next
   */
  gUserCount: (request, response, next) => {
    let {sex, business, username_or_name} = request.body;
    User.getUserCount(sex, business, username_or_name)
      .then(result => {
        request.userCount = result;
        next();
      })
      .catch(error => {
        next(error);
      })
  },

  /**
   * 获取指定的用户中间件
   * @param request
   * @param response
   * @param next
   */
  gUserRowCountRecord: (request, response, next) => {
    let start = response.start;
    let size = response.size;

    let {sex, business, username_or_name} = request.body;

    User.getUserRowCountRecord(start, size, sex, business, username_or_name)
      .then(result => {
        request.recordCountUser = result;
        next();
      })
      .catch(error => {
        next(error);
      })

  },

  /**
   * 用户注册中间件
   * @param request
   * @param response
   * @param next
   */
  sUser: (request, response, next) => {
    let {username, password} = request.body
    let user = {
      username,
      password
    }

    /**把用户写入数据库*/
    User.setUser(user)
      .then(results => { // 注册成功
        request.add_uid = results;
        request.user = user;

        fs.mkdir('static/upload/user/'+username , function(){   //进行创建用户目录
          console.log('成功创建用户<'+username+'>目录');
          console.log();
          console.log();
          console.log();
        });
        next();
      })
      .catch(error => { // 注册失败
        next(error);
      })

  },

  /**
   * 更新用户信息
   * @param request
   * @param response
   * @param next
   */
  uUser: (request, response, next) => {
    let {username, password, name, sex, birthday, tel, address, money, business} = request.body;

    let user = {username, password, name, sex, birthday, tel, address, money, business}

    console.log('username=' + user.username + ';password=' + user.password + ';name=' + user.name+';sex=' + user.sex + ';birthday=' + user.birthday
      + ';tel=' + user.tel + ';address=' + user.address + ';money=' + user.money +
      ';business=' + user.business + '--------------------------');
    User.updateUser(user)
      .then(result => {
        request.affectR = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 删除用户
   * @param request
   * @param response
   * @param next
   */
  dUser: (request, response, next) => {
    let {username} = request.body;
    User.deleteUser(username)
      .then(result => {
        request.affectR = result;    //把受影响的行数返回到request中
        next();
      })
      .catch(error => {
        next(error);
      })
  }

}